﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Linq;

namespace XML_PARSING
{
    class CatalogueParsing
    {
        
        static void ParseDom()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("../../catalogue.xml");

            XmlNode rootNode = doc.DocumentElement;
            Console.WriteLine("Root node: {0}", rootNode.Name);

            Dictionary<string, int> hashtable = new Dictionary<string, int>();

            foreach (XmlNode node in rootNode.ChildNodes)
            {
                string artist = node["artist"].InnerText;
                if (!hashtable.ContainsKey(artist))
                {
                    hashtable.Add(artist, 1);
                }
                else
                {
                    hashtable[artist]++;
                }

            }

            foreach (var item in hashtable)
            {
                Console.WriteLine(item.Key + " "+item.Value);
            }
            

        }
        static void ParseXpath()
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("../../catalogue.xml");
            string xPathQuery = "/albums/album";

            XmlNodeList nodelist = xmlDoc.SelectNodes(xPathQuery);


            Dictionary<string, int> hashtable = new Dictionary<string, int>();

            foreach (XmlNode node in nodelist)
            {
                string artist = node["artist"].InnerText;
                if (!hashtable.ContainsKey(artist))
                {
                    hashtable.Add(artist, 1);
                }
                else
                {
                    hashtable[artist]++;
                }

            }

            foreach (var item in hashtable)
            {
                Console.WriteLine(item.Key + " " + item.Value);
            }
            
            
 
        }
        static void DomDeleteExpensiveAlbums()
        {
            const double EXPENSIVE_PRICE = 23;
            XmlDocument doc = new XmlDocument();
            doc.Load("../../catalogue.xml");

            XmlNode rootNode = doc.DocumentElement;

            XmlNode node = rootNode.FirstChild;
            while (node!=null)
            {
                double price = double.Parse(node["price"].InnerText.Trim());
                Console.WriteLine(price);

                if (price >= EXPENSIVE_PRICE)
                {
                    XmlNode temp = node;
                    node = node.NextSibling;
                    rootNode.RemoveChild(temp);
                }
                else
                {
                    node = node.NextSibling;
                }
            }

            foreach (XmlNode n in rootNode.ChildNodes)
            {
                Console.WriteLine(n.InnerText);
            }
             

        }
        static void XMLReaderAllSongsTitle()
        {
            using (XmlReader reader = XmlReader.Create("../../catalogue.xml"))
            {
              while (reader.Read()) 
              {
                  if ((reader.NodeType == XmlNodeType.Element) &&
                   (reader.Name == "title"))
                  {
                      Console.WriteLine(reader.ReadElementString());
                  }
              }
            }

        }
        static void XDocumentAllSongsTitle()
        {
            XDocument xmlDoc = XDocument.Load("../../catalogue.xml");

            var titles =
              from a in xmlDoc.Descendants("title")
              select a.Value;            

            foreach (var title in titles)
            {
                Console.WriteLine(title);
            }

        }

        static void CreateXMLWithDOM()
        {
            XmlDocument newXML = new XmlDocument();

            XmlElement parentNode = newXML.CreateElement("Person");
            newXML.DocumentElement.PrependChild(parentNode);

        
            XmlElement persons = newXML.CreateElement("persons");

            newXML.AppendChild(persons);

            newXML.Save("../../personinfo.xml");


        }

        static void Main(string[] args)
        {
            //ParseDom();
            //ParseXpath();
            //DomDeleteExpensiveAlbums();
            //XMLReaderAllSongsTitle();
            //XDocumentAllSongsTitle();
            //CreateXMLWithDOM();

        }
    }
} 
